package com.factory.iamp.pojo;

import com.alibaba.fastjson.JSONObject;
import com.factory.iamp.utils.helper.SetMethodHelper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import static com.factory.iamp.utils.helper.id2nameHelper.findObjectName;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class TableForStoreList {
    private int tid;
    private int ftid;
    private int lid;
    private String pre_cast_id;
    private String sell_id;
    private String name;
    private String creator;
    private UserName creator_info;
    private String handler;
    private UserName handler_info;
    private int type;
    private int header_id;
    private String header_value;
    private List<Integer> body_list;
    private int unfinished_content;
    private String unfinished_content_name;
    private int unfinished_question;
    private String body_value;
    private int tail_id;
    private String tail_value;
    private String problem;
    private String file_path;
    private String return_reason;
    private String return_process;
    private Timestamp created_at;
    private Timestamp updated_at;
    private int can_delete;

    public TableForStoreList(JSONObject data) {
        this.SetAll(data);
    }

    public TableForStoreList(Table data) {
        this.tid = data.getTid();
        this.pre_cast_id = data.getPre_cast_id();
        this.sell_id = data.getSell_id();
        this.name = data.getName();
        this.creator = data.getCreator();
        this.creator_info = data.getCreator_info();
        this.handler = data.getHandler();
        this.handler_info = data.getHandler_info();
        this.type = data.getType();
        this.header_id = data.getHeader_id();
        this.header_value = data.getHeader_value();
        this.body_list = data.getBody_list();
        this.unfinished_content = data.getUnfinished_content();
        this.unfinished_content_name = data.getUnfinished_content_name();
        this.unfinished_question = data.getUnfinished_question();
        this.body_value = data.getBody_value();
        this.tail_id = data.getTail_id();
        this.tail_value = data.getTail_value();
        this.problem = data.getProblem();
        this.file_path = data.getFile_path();
        this.return_reason = data.getReturn_reason();
        this.return_process = data.getReturn_process();
        this.created_at = data.getCreated_at();
        this.updated_at = data.getUpdated_at();
//        this.SetAll((JSONObject) JSONObject.toJSON(data));
    }

    public void SetAll(JSONObject data) {
        if(data.containsKey("tid")) this.tid = data.getInteger("tid");
        if(data.containsKey("pre_cast_id")) this.pre_cast_id = data.getString("pre_cast_id");
        if(data.containsKey("sell_id")) this.name = data.getString("sell_id");
        if(data.containsKey("name")) this.name = data.getString("name");
        if(data.containsKey("creator")) this.creator = data.getString("creator");
        if(data.containsKey("handler")) this.handler = data.getString("handler");
        if(data.containsKey("type")) this.type = data.getInteger("type");
        if(data.containsKey("header_id")) this.header_id = data.getInteger("header_id");
        if(data.containsKey("header_value")) this.setHeader_value(data.getJSONObject("header_value"));
        if(data.containsKey("body_list")) this.setBody_list(data.getString("body_list"));
        if(data.containsKey("body_value")) this.setBody_value(data.getJSONObject("body_value"));
        if(data.containsKey("tail_id")) this.tail_id = data.getInteger("tail_id");
        if(data.containsKey("tail_value")) this.setTail_value(data.getJSONObject("tail_value"));
        if(data.containsKey("problem")) this.problem = data.getString("problem");
        if(data.containsKey("file_path")) this.file_path = data.getString("file_path");
    }

    public void setUnfinished_content(int unfinished_content) {
        this.unfinished_content = unfinished_content;
        this.unfinished_content_name = findObjectName(unfinished_content);
    }

    public void setCreator_info(User user) {
        this.creator_info = new UserName(user);
    }

    public void setHandler_info(User user) {
        this.handler_info = new UserName(user);
    }

    public void setHeader_value(Object value) {
        this.header_value = SetMethodHelper.SetString(value);
    }

    public void setBody_value(Object value) {
        this.body_value = SetMethodHelper.SetString(value);
    }

    public void setTail_value(Object value) {
        this.tail_value = SetMethodHelper.SetString(value);
    }

    public void setBody_list(String value) {
        if(value.length() < 1) this.body_list = new ArrayList<>();
        else this.body_list = JSONObject.parseArray(value, Integer.class);
    }

    public void insertCheck() {
        if(this.body_list == null) this.body_list = new ArrayList<>();
    }
}
